package gsw.demo.mvp.core.net.service;

import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.Toast;

import java.util.HashMap;

import gsw.demo.mvp.R;
import gsw.demo.mvp.core.net.service.callback.IError;
import gsw.demo.mvp.core.net.service.callback.IFailure;
import gsw.demo.mvp.core.net.service.callback.ISuccess;

public class ActivityRetrofitService extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_retroft_origin);
    }

    /**
     * get请求
     * 需要在Applicaiton中初始化Config:
     * ConfigInit.init(this).withApiHost("http://dengpaoedu.com:8080/").configure();
     */
    public void click1(View view) {
        HashMap params = new HashMap();
        params.put("cityname", "长沙");
        params.put("key", "fd0f609b22905a0a56a48d7cf59a558b");
        RestClient.create()
                .url("/weather")
                .params(params)
                .success(new ISuccess() {
                    @Override
                    public void onSuccess(String response) {
                        Toast.makeText(ActivityRetrofitService.this, "请求成功:" + response, Toast.LENGTH_SHORT).show();
                    }
                })
                .error(new IError() {
                    @Override
                    public void onError(int code, String msg) {
                        Toast.makeText(ActivityRetrofitService.this, "请求错误:" + msg, Toast.LENGTH_SHORT).show();
                    }
                })
                .failure(new IFailure() {
                    @Override
                    public void onFailure() {
                        Toast.makeText(ActivityRetrofitService.this, "请求失败", Toast.LENGTH_SHORT).show();
                    }
                })
                .build().get();
    }

    /**
     * 文件上传
     */
    public void click2(View view) {
        //上传路径：http://dengpaoedu.com:8080/fileuploadanddownload/uploadServlet.
        HashMap params = new HashMap();
        params.put("file", "abcd.txt");
        RestClient.create()
                .params(params)
                .url("/fileuploadanddownload/uploadServlet")
                .file(Environment.getExternalStorageDirectory().getAbsolutePath() + "/test.txt")
                .success(new ISuccess() {
                    @Override
                    public void onSuccess(String response) {
                        Toast.makeText(ActivityRetrofitService.this, "请求成功:" + response, Toast.LENGTH_SHORT).show();
                    }
                })
                .error(new IError() {
                    @Override
                    public void onError(int code, String msg) {
                        Toast.makeText(ActivityRetrofitService.this, "请求错误:" + msg, Toast.LENGTH_SHORT).show();
                    }
                })
                .failure(new IFailure() {
                    @Override
                    public void onFailure() {
                        Toast.makeText(ActivityRetrofitService.this, "请求失败", Toast.LENGTH_SHORT).show();
                    }
                }).build().upload();
    }

    /**
     * 文件下载
     */
    public void click3(View view) {
        HashMap params = new HashMap();
        params.put("file", "abcd.txt");
        //测试下载路径：http://dengpaoedu.com:8080/examples/test.zip
        RestClient.create()
                .params(params)
                .url("/examples/test.zip")
                .dir("/sdcard")
                .extension("zip")
                .success(new ISuccess() {
                    @Override
                    public void onSuccess(String response) {
                        Toast.makeText(ActivityRetrofitService.this, "请求成功:" + response, Toast.LENGTH_SHORT).show();
                    }
                })
                .error(new IError() {
                    @Override
                    public void onError(int code, String msg) {
                        Toast.makeText(ActivityRetrofitService.this, "请求错误:" + msg, Toast.LENGTH_SHORT).show();
                    }
                })
                .failure(new IFailure() {
                    @Override
                    public void onFailure() {
                        Toast.makeText(ActivityRetrofitService.this, "请求失败", Toast.LENGTH_SHORT).show();
                    }
                })
                .build().download();
    }
}
